﻿using System;
using System.Configuration;
using CoreEx.Common.Ioc;
using LinFu.IoC.Configuration;
using LinFu.IoC.Interfaces;

namespace CoreEx.Wcf.Common.Implementation
{
    /// <summary>
    /// Provides a default <see cref="Uri"/> for the requested service type that is 
    /// used only when no other <see cref="Uri"/> service can be found.
    /// The implementation relies on service urls to be located in the app/web configuration file.
    /// </summary>
    /// <remarks>
    /// Because the <see cref="Uri"/> may be different for each service type, the requested service type name 
    /// is available through <see cref="IServiceInfo.ServiceName"/> property.
    /// If you want to use another <see cref="Uri"/>, implement the <see cref="IFactory{T}"/> interface 
    /// to create an appropriate <see cref="Uri"/> instance according to the service type name.
    /// </remarks>
    [PostProcessor]
    public class UriPostProcessor : PostProcessorBase<Uri>
    {
        public override void Process(IServiceRequestResult result)
        {
            if (result.OriginalResult == null)
                result.ActualResult = new Uri(string.Format("{0}Services/{1}.svc",ConfigurationManager.AppSettings["DefaultServer"],((Type)result.AdditionalArguments[0]).FullName));                
        }
    }
}